package table_Mixage;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.lang.Math;

/**
 *  Dessin
 *
 * Description: Classe qui dessine le Spectrogramme et les fonction associé (barre de progression, selection).
 * 
 * @author Damien Deldon</a>
 * @version 1.0
 */

public class Dessin extends JPanel implements MouseListener{
	
	private static final long serialVersionUID = 8905201156332244620L;

	Graphics g;
	char c= 's';
	private Piste piste;
	double coefmax, dureed =0;
	public int xMax = 0;
	public int yMax = 0;
	int pointBarre = 0;
	public int m;
	private int numCanal = 0;
	int positionX = 0;
	int positionX2 = 0;
	
	
	public void paint(Graphics g) {
		
		this.addMouseListener(this);
		g.setColor(Color.BLACK);
		g.fillRect(0, 0, 500, 100);
		g.setColor(Color.GRAY);
		this.xMax = getSize().width-5;
		int x=0;
		float tmp=0;
		yMax = getSize().height;
	    g.drawLine(0, 50, 500, 50);
	    g.setColor(Color.YELLOW);
	   
	    if (piste != null){
	    	
	    	tmp =  this.piste.getDuree(0);
	    	StringBuffer sb = new StringBuffer();
	    	if ( tmp > 60){
	    		sb.append((int)(tmp/60)+":"+(((tmp/60)- (int)(tmp/60))*60));
	    		 dureed =((double)tmp/(double)60)+ ((double)tmp%(double)60)*0.006;
	    		 c='m';

	    	} else{
	    		sb.append("00"+":"+(tmp%60));
	    		 dureed = (double)tmp +(double)tmp/(double)60;
	    		 c='s';
	    	}
		    String temps = sb.toString();
	    	g.drawString(temps, 450, 25);
	    }else{
	    		g.drawString("00:00.0", 450, 24);
	    	}
	    for (int i =0; i<500;i=i+15){
	    	g.drawLine(i,25,i+5,25);
	    	g.drawLine(i, 75, i+5, 75);
	    }
	    int n=0,w=0;
	    	if(piste != null){
		    	x = 0;
		    	double valmax = Math.pow(2,(piste.getResolution()*8)-1);
		    	this.coefmax = (double)yMax/(double)valmax;
		    	m= piste.getCanal(numCanal).getNbech()/xMax;;
		    	while ( x < xMax) {
		    		// affichage des barres temporelles
		    		g.setColor(Color.YELLOW);
		    		if(((x)%(xMax/dureed))< 1){
		    			g.drawLine(x, 0, x, 10);
		    			g.drawString(w+String.valueOf(c), x+5, yMax-1);
		    			w++;
		    			g.drawLine(x, yMax, x, yMax -10);
		    		}
		    		g.setColor(Color.GREEN);
		    		g.drawLine(x, (int)(piste.getCanal(numCanal).getEchantillon(n)*coefmax +50), x+1, (int)((piste.getCanal(numCanal).getEchantillon(n+m)*coefmax)+50));
		    		x++;
		    		if (n+m <= piste.getCanal(numCanal).getNbech())
		    			n=n+m;
		    		else
		    			n=piste.getCanal(numCanal).getNbech();
		    		
		    		if(x == pointBarre && pointBarre != 0){
		    			g.setColor(Color.white);
		    			g.drawLine(x, 0,x, 100);
		    			g.drawLine(x+1, 0,x+1,100);
		        		g.setColor(Color.GREEN);
		    		}
					if(positionX != 0){
						g.setColor(Color.red);
						g.drawLine(positionX,0, positionX, 100);
					}
					if(positionX2 != 0 && positionX < positionX2){
						g.setColor(Color.red);
						g.drawLine(positionX2,0, positionX2, 100);
						g.setColor(new Color(.3f, .4f, .5f, 0.01f));
						g.fillRect(positionX, 0,  positionX2 - positionX, 100);
					}
		    	}
		    }
	} 
	@Override
	public void mouseClicked(MouseEvent e) {
		
	}
	@Override
	public void mouseEntered(MouseEvent e) {
		
	}
	@Override
	public void mouseExited(MouseEvent e) {
		
	}
	@Override
	public void mousePressed(MouseEvent e) {
		this.positionX = e.getX();
		this.updateUI();
	}
	@Override
	public void mouseReleased(MouseEvent e) {
		this.positionX2 = e.getX();
		this.updateUI();
	}
	
//Seteur
	
	void paintBarre(int x){
		this.pointBarre = x;
    	this.updateUI();
	}
	public void setPiste(Piste p){
		this.piste = p;
	}
	public void setNumCanal(int n){
		this.numCanal = n;
	}
}